<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .wrap {
      width: 80%;
      margin: 20px auto;
      display: flex;
      position: relative;
    }

    #small {
      width: 400px;
      height: 400px;
      position: relative;
      text-align: center;
      margin-left: 50px;
    }

    #small img {
      width: 400px;
      height: 400px;
    }

    #mask {
      width: 150px;
      height: 150px;
      background: rgba(255, 255, 255, 0.5);
      cursor: move;
      position: absolute;
      top: 0;
      left: 0;
      /* display: none; */
    }

    #big {
      width: 500px;
      height: 500px;
      float: left;
      overflow: hidden;
      margin-left: 50px;
      position: absolute;
      left: 400px;
      top: 0;
      display: none;
    }

    #big img {
      width: 1200px;
      height: 1200px;
    }

    .info {
      height: 400px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
    }

    .info input {
      width: 80px;
    }

    .price {
      color: red;
    }

    .oldprice {
      color: #666;
      text-decoration: line-through;
    }
  </style>
</head>

<body>
  <h1>详情页</h1>
  <div class="wrap">
    <div id="small">
      <img src="./images/bx1.jpg" alt="">
      <div id="mask"></div>
    </div>
    <div id="big">
      <img src="./images/bx1.jpg" alt="">
    </div>
    <div class="info">
      <!-- <h1>商品名称:</h1>
      <h2>价格：<span class="price">0</span></h2>
      <h1>原价:<span class="oldprice">0</span></h1>
      <h2>描述：</h2>
      <h2>库存</h2>
      <h2>
        <button>-</button><input type="text" value="0"> <button>+</button>
      </h2>
      <button>加入购物车</button> -->
    </div>
  </div>
  <script src="./jquery-3.4.1.min.js"></script>
  <script>
    // 获取地址栏中的id参数

    function getParms(key) {
      var search = location.search.substr(1);
      var arr = search.split("&");//["pid=12313",age=2,sex=3]
      for (var i = 0; i < arr.length; i++) {
        var newarr = arr[i].split("=") //[pid,12313] [age,2]
        if (key == newarr[0]) {
          return newarr[1]
        }
      }
    }
    var pid = getParms("pid")
    console.log(pid)
    // 加载详情
    function loadInfo() {
      $.ajax({
        url: "http://127.0.0.1:3000/product/findProduct",
        type: "post",
        data: {
          id: pid
        },
        success: function (res) {
          console.log(res)

          $("#small img").attr("src", `./images/${res.img}.jpg`) //设置小图
          $("#big img").attr("src", `./images/${res.img}.jpg`) //设置大图

          var str = ` <h1>${res.pname}</h1>
            <h2>价格：<span class="price">￥${res.price}</span></h2>
            <h1>原价:<span class="oldprice">￥${res.oldprice}</span></h1>
            <h2>描述：${res.des}</h2>
            <h2>库存:${res.inventory}</h2>
            <h2>
             数量： <button class="prev">-</button><input type="text" value="1" id="mynum"> <button class="next">+</button>
            </h2>
            <button id="addBtn">加入购物车</button>`
          $(".info").html(str)
          // 更改数量---------------------------------------
          $(".prev").click(function () { //减
            var num = Number($(this).next().val())
            num--;
            if (num < 1) {
              num = 1;
            }
            $(this).next().val(num)
          })
          $(".next").click(function () {
            var num = Number($(this).prev().val())
            num++;
            if (num > res.inventory) {
              num = res.inventory
              alert("超出购买数量")
            }
            $(this).prev().val(num)
          })
          // -------------------------------------

          // 添加购物车
          $("#addBtn").click(function () {
            var data = {
              token: localStorage.token,////获取localStroage中的用户token 发送给后台
              uid: localStorage.uid,//获取localStroage中的用户id 发送给后台
              des: res.des,
              img: res.img,
              inventory: res.inventory,
              oldprice: res.oldprice,
              oneId: res.oneId,
              pname: res.pname,
              price: res.price,
              twoId: res.twoId,
              pid: res._id, //商品id
              num: $("#mynum").val() //商品数量
            }
            $.ajax({
              url: "http://127.0.0.1:3000/cart/addCart",
              type: "post",
              data: data,
              success: function (res) {
                console.log(res)
                alert("添加购物车成功")
              }
            })
          })



        }
      })

    }
    loadInfo()
















    //小盒子的鼠标移动事件
    $("#small").mousemove(function (e) {
      // e.pageX e.pageY 
      var x = e.pageX - $("#small").offset().left - $("#mask").width() / 2;
      var y = e.pageY - $("#small").offset().top - $("#mask").height() / 2;
      // 限制最小
      x < 0 ? x = 0 : x;
      y < 0 ? y = 0 : y;
      // 限制最大
      x > $("#small").width() - $("#mask").width() ? x = $("#small").width() - $("#mask").width() : x;
      y > $("#small").height() - $("#mask").height() ? y = $("#small").height() - $("#mask").height() : y
      $("#mask").css({
        left: x + "px",
        top: y + "px"
      })
      // 算出大图片和小盒子的宽高比
      var bilix = $("#big img").width() / $("#small").width();
      var biliy = $("#big img").height() / $("#small").height();
      //成比例  改变大盒子的scrollTop
      $("#big").scrollLeft(bilix * x)
      $("#big").scrollTop(biliy * y)
    })
    // 鼠标移入移出的显示隐藏
    $("#small").mouseenter(function () {
      $("#big").show();
      $("#mask").show()
    }).mouseleave(function () {
      $("#big").hide();
      $("#mask").hide()
    })


  </script>
</body>

</html>